package com.firexxx.vp.infrastructure.query.impl;

import com.firexxx.common.domain.query.CacheConsistencyInterface;
import com.firexxx.common.utils.JacksonConverter;
import com.firexxx.vp.adapter.Constants;
import com.firexxx.vp.adapter.scheduled.CacheSynchronizationManager;
import com.firexxx.vp.domain.vp.VoicePrintModel;
import com.firexxx.vp.domain.vp.fuse.FuseVoicePrintEntity;
import com.firexxx.vp.domain.vp.fuse.FuseVoicePrintRepository;
import com.firexxx.vp.domain.vp.query.VoicePrintModelQueryInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2020/02/16 13:45
 */
@Slf4j
public class VoicePrintModelRedisCache implements VoicePrintModelQueryInterface, CacheConsistencyInterface {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CacheSynchronizationManager cacheSynchronizationManager;

    @Autowired
    private FuseVoicePrintRepository fuseVoicePrintRepository;

    //TODO 管道
    @Override
    public List<VoicePrintModel> findByVpIdList(List<String> vpIdList) throws Exception {
        List<VoicePrintModel> voicePrintModelList = new ArrayList<>();
        for (String vpId : vpIdList) {
            String result = stringRedisTemplate.opsForValue().get(Constants.APP_MAGIC + ":"
                    + vpId);
            VoicePrintModel voicePrintModel = JacksonConverter.toObject(result, VoicePrintModel.class);
            voicePrintModel.translate();

            voicePrintModelList.add(voicePrintModel);
        }

        return voicePrintModelList;
    }

    @Override
    public List<VoicePrintModel> findByGroupId(String groupId) throws Exception {
        throw new Exception("未实现的方法");
    }

    @Override
    public void register() {
        cacheSynchronizationManager.getCacheBeanList().add(this);
    }

    @Override
    public void consistency() {
        List<FuseVoicePrintEntity> fuseVoicePrintEntityList = fuseVoicePrintRepository.findAll();

        for (FuseVoicePrintEntity fuseVoicePrintEntity : fuseVoicePrintEntityList) {
            try {
                String result = stringRedisTemplate.opsForValue().get(Constants.APP_MAGIC + ":"
                        + fuseVoicePrintEntity.getVpModel().getVpId());

                VoicePrintModel voicePrintModel = JacksonConverter.toObject(result, VoicePrintModel.class);
                if (!stringRedisTemplate.hasKey(Constants.APP_MAGIC + ":"
                        + voicePrintModel.getVpId())) {
                    stringRedisTemplate.opsForValue().set(Constants.APP_MAGIC + ":"
                            + voicePrintModel.getVpId(),
                            JacksonConverter.toJson(voicePrintModel));

                    log.info("重新同步缓存不一致的vpId:{}", voicePrintModel.getVpId());
                }
            } catch (Exception e) {
                log.error("vpId {} error : {}", fuseVoicePrintEntity.getVpModel().getVpId(), e);
            }
        }
    }
}